package scau.redi.social_platform.common.handler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import scau.redi.social_platform.common.exception.AuthException;
import scau.redi.social_platform.common.staticCode.ResponseCode;
import scau.redi.social_platform.common.util.ResponseUtil;

import java.util.Map;

/**
 * @Author: Redi
 * @Date: 2022/5/18 20:39
 * @Description:
 */
@ControllerAdvice
@RestController
public class AuthExceptionHandler {

    /**
     * 拦截自定义异常
     * @param ex
     * @return
     */
    @ExceptionHandler(AuthException.class)
    @ResponseBody
    public Map<String, Object> exceptionHandler(AuthException ex){
        ex.printStackTrace();
        if(ex.getCode()==null){
            ex.setCode(4);
        }
        return ResponseUtil.fail(ResponseCode.NEED_LOGIN.getDesc(),ResponseCode.NEED_LOGIN.getCode());

    }

}

